package com.example.demo.service.impl;

import com.example.demo.mapper.wanfadao;
import com.example.demo.pojo.liaotianneirong;
import com.example.demo.pojo.wanfaa;
import com.example.demo.service.sv.wanfa;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class wanfaimpl implements wanfa {
    @Autowired
    wanfadao wanfadao;
    @Override
    public wanfaa kaishipipei(String zhang) {

        wanfaa wanfaa =wanfadao.chapipeizhuangtai(zhang);
           if(wanfaa!=null)
               return wanfaa;
           else {
               wanfaa wanfaa1=wanfadao.chakongfang();

               if(wanfaa1!=null) {
                   int a=0;
                   if(wanfaa1.getZhang1()!=null)
                       a+=1;
                   if(wanfaa1.getZhang2()!=null)
                       a+=1;
                   if(wanfaa1.getZhang3()!=null)
                       a+=1;
                   if(wanfaa1.getZhang1()==null) {
                       wanfadao.updatezhang1(wanfaa1.getFangjianid(),zhang);
                         if(a==2)
                       wanfadao.updatezhuangtai(wanfaa1.getFangjianid());
                   }
                   else if (wanfaa1.getZhang2()==null) {
                       wanfadao.updatezhang2(wanfaa1.getFangjianid(), zhang);
                       if(a==2)
                           wanfadao.updatezhuangtai(wanfaa1.getFangjianid());
                   }
                   else if(wanfaa1.getZhang3()==null) {

                           wanfadao.updatezhang3(wanfaa1.getFangjianid(),zhang);
                           if(a==2)
                               wanfadao.updatezhuangtai(wanfaa1.getFangjianid());

                   }

               }
               else {
                     int b;

                    if(wanfadao.chamaxrow()!=null)
                         b=wanfadao.chamaxrow()+1;
                    else
                        b=1;
                   wanfadao.insertzhang1(b,zhang);
               }
               wanfaa1=wanfadao.chapipeizhuangtai(zhang);
               return wanfaa1;
           }


    }

    @Override
    public int  jieshupipei(String zhang) {
        wanfaa wanfaa=wanfadao.chapipeizhuangtai(zhang);
           int a=0;
           if(wanfaa.getZhang1()!=null)
               a+=1;
           if(wanfaa.getZhang2()!=null)
            a+=1;
           if(wanfaa.getZhang3()!=null)
            a+=1;

           if(a==1)
               wanfadao.delete(wanfaa.getFangjianid());
           else
           {   if(Objects.equals(wanfaa.getZhang1(), zhang))
                  wanfadao.deletezhang1(zhang,wanfaa.getFangjianid());
               else if(Objects.equals(wanfaa.getZhang2(), zhang))
                   wanfadao.deletezhang2(zhang,wanfaa.getFangjianid());
               else if(Objects.equals(wanfaa.getZhang3(), zhang))
                   wanfadao.deletezhang3(zhang,wanfaa.getFangjianid());

           }
           return a;
    }

    @Override
    public void qiangdizhu(int fangjianid, int xuhao) {
        wanfaa wanfaa=wanfadao.cha(fangjianid);
        if(xuhao==1)
            wanfadao.updatejiaodizhu1(fangjianid,wanfaa.getJiao1()+1,wanfaa.getJiaofencishu()-1);
       else if(xuhao==2)
            wanfadao.updatejiaodizhu2(fangjianid,wanfaa.getJiao2()+1,wanfaa.getJiaofencishu()-1);
        else if(xuhao==3)
            wanfadao.updatejiaodizhu3(fangjianid,wanfaa.getJiao3()+1,wanfaa.getJiaofencishu()-1);
    }
    @Override
    public void buqiangdizhu(int fangjianid, int xuhao) {
        wanfaa wanfaa=wanfadao.cha(fangjianid);
        if(xuhao==1)
            wanfadao.updatebujiaodizhu1(fangjianid,-1,wanfaa.getJiaofencishu()-1);
        else if(xuhao==2)
            wanfadao.updatebujiaodizhu2(fangjianid,-1,wanfaa.getJiaofencishu()-1);
        else if(xuhao==3)
            wanfadao.updatebujiaodizhu3(fangjianid,-1,wanfaa.getJiaofencishu()-1);
    }

    @Override
    public void fenpai(int fangjianid,int xuhao) {

        if(xuhao==1)
        {
            List<Integer> list=new ArrayList<>();
            List<Integer> list1=new ArrayList<>();
            List<Integer> list2=new ArrayList<>();
            List<Integer> list3=new ArrayList<>();
            List<Integer> listdizhu=new ArrayList<>();

            for(int b=0;b<=54;b++)
                list.add(b);

            for(int b=1;b<=3;b++){
                for(int c=1;c<=17;c++){
                    int d=(int)Math.floor(Math.random()*54+1);
                       if(list.get(d)!=0)
                         {  list.set(d,0);
                             if(d>13&&d<53){
                                 d=d%13;
                                 if(d==0)
                                     d=13;
                             }
                             if(b==1){
                                 list1.add(d);
                             }
                            else if(b==2){
                                 list2.add(d);
                             }
                            else if(b==3){
                                 list3.add(d);
                             }

                          }
                       else if(list.get(d)==0){
                           c--;
                       }

                 }
            }

            for(int c=1;c<=3;c++)
            {  int d=(int)Math.floor(Math.random()*54+1);
                if(list.get(d)!=0){
                    list.set(d,0);
                    if(d>13&&d<53){
                        d=d%13;
                        if(d==0)
                            d=13;
                    }
                    listdizhu.add(d);
                }
                else if (list.get(d)==0) {
                    c--;
                }


            }
            Collections.sort(listdizhu);
            Collections.sort(list1);
            Collections.sort(list2);
            Collections.sort(list3);
            String zhang1=list1.stream().map(String::valueOf).collect(Collectors.joining(","));
            String zhang2=list2.stream().map(String::valueOf).collect(Collectors.joining(","));
            String zhang3=list3.stream().map(String::valueOf).collect(Collectors.joining(","));
            String dizhu=listdizhu.stream().map(String::valueOf).collect(Collectors.joining(","));
          wanfadao.updateshoupai(fangjianid,zhang1,zhang2,zhang3,dizhu);
        }


    }
    @Override
    public wanfaa  querendizhu(int fangjianid){
        wanfaa wanfaa=wanfadao.cha(fangjianid);
        if((wanfaa.getJiao1()>wanfaa.getJiao2())&&(wanfaa.getJiao1()>wanfaa.getJiao3())) {
            wanfadao.updatedizhu1(fangjianid, 1);
            String[] list=wanfaa.getDizhupai().split(",");
            String[] list1=wanfaa.getShoupai1().split(",");
            List<Integer> l=new ArrayList<>();
           for(String s :list){
               l.add(Integer.parseInt(s));
           }
            for(String s :list1){
                l.add(Integer.parseInt(s));
            }
            Collections.sort(l);
            String lll=l.stream().map(String::valueOf).collect(Collectors.joining(","));
            wanfadao.updateshoupai1(fangjianid,lll);

        }

        else if((wanfaa.getJiao2()>wanfaa.getJiao1())&&(wanfaa.getJiao2()>wanfaa.getJiao3())) {
            wanfadao.updatedizhu2(fangjianid, 2);
            String[] list=wanfaa.getDizhupai().split(",");
            String[] list1=wanfaa.getShoupai2().split(",");
            List<Integer> l=new ArrayList<>();
            for(String s :list){
                l.add(Integer.parseInt(s));
            }
            for(String s :list1){
                l.add(Integer.parseInt(s));
            }
            Collections.sort(l);
            String lll=l.stream().map(String::valueOf).collect(Collectors.joining(","));
            wanfadao.updateshoupai2(fangjianid,lll);

        }

        else if((wanfaa.getJiao3()>wanfaa.getJiao2())&&(wanfaa.getJiao3()>wanfaa.getJiao1())) {
            wanfadao.updatedizhu3(fangjianid, 3);
            String[] list=wanfaa.getDizhupai().split(",");
            String[] list1=wanfaa.getShoupai3().split(",");
            List<Integer> l=new ArrayList<>();
            for(String s :list){
                l.add(Integer.parseInt(s));
            }
            for(String s :list1){
                l.add(Integer.parseInt(s));
            }
            Collections.sort(l);
            String lll=l.stream().map(String::valueOf).collect(Collectors.joining(","));
            wanfadao.updateshoupai3(fangjianid,lll);

        }

         return wanfadao.cha(fangjianid) ;
    }

    @Override
    public wanfaa fangjianxinxi(int fangjianid) {
        return wanfadao.cha(fangjianid);
    }

    @Override
    public void chupai(int fangjianid, int xuhao, int chupaishu, String chupai) {
         wanfaa wanfaa=wanfadao.cha(fangjianid);
          if(xuhao==1){
              wanfadao.updatechupai1(fangjianid,wanfaa.getPaishu1()-chupaishu,chupai);
          }
       else if(xuhao==2){
              wanfadao.updatechupai2(fangjianid,wanfaa.getPaishu2()-chupaishu,chupai);
          }
        else if(xuhao==3){
              wanfadao.updatechupai3(fangjianid,wanfaa.getPaishu3()-chupaishu,chupai);
          }
    }

    @Override
    public void buyao(int fangjianid, int xuhao) {
        wanfaa wanfaa=wanfadao.cha(fangjianid);

            if (xuhao == 1)
                wanfadao.updatebuyao1(fangjianid, wanfaa.getBuyaocishu() + 1);
            else if (xuhao == 2)
                wanfadao.updatebuyao2(fangjianid, wanfaa.getBuyaocishu() + 1);
            else if (xuhao == 3)
                wanfadao.updatebuyao3(fangjianid, wanfaa.getBuyaocishu() + 1);

         if ((wanfaa.getBuyaocishu()+1)==2) {
              wanfadao.updateqingkongdipai(fangjianid);
        }

    }

    @Override
    public int bidaxiao(int fangjianid, List<Integer> chupai) {
        String cp=null;
        int shanghaizhi=0,guanjianzhi=0,g1=0,g2=0,g3=0,g4=0;
        int c=1,z=0;
        List<Integer> list=new ArrayList<>();
           list.add(chupai.get(0));
           for(int a=0;a<chupai.size();a++)
           {   if((a+1)<chupai.size())
                {   if(Objects.equals(chupai.get(a), chupai.get(a + 1)))
                            c+=1;
                      else
                       {
                           list.add(chupai.get(a+1));
                           switch (c) {
                               case 1 -> g1 += 1;
                               case 2 -> g2 += 1;
                               case 3 -> g3 += 1;
                               case 4 -> g4 += 1;
                           }
                           c=1;
                       }
                }
                else if(a==(chupai.size()-1))
                     {
                         switch (c) {
                             case 1 -> g1 += 1;
                             case 2 -> g2 += 1;
                             case 3 -> g3 += 1;
                             case 4 -> g4 += 1;
                         }
                     }
           }
             if(g1==1&&g2==0&&g3==0&&g4==0) {
                 shanghaizhi=1;
                   cp="dan";
                   guanjianzhi=chupai.get(0);
             }
             else if(g1==2&&g2==0&&g3==0&&g4==0){
                 if((list.get(0)==53)&&(list.get(1)==54)) {
                     shanghaizhi = 3;
                     cp = "wangzha";
                 }
             }
             else if (g1==0&&g2==1&&g3==0&&g4==0) {
                     guanjianzhi=list.get(0);
                     shanghaizhi=1;
                     cp="dui";
             }
             else if (g1==0&&g2==0&&g3==1&&g4==0) {
                 shanghaizhi=1;
                 cp="san";
                 guanjianzhi=chupai.get(0);
             }
             else if (g1==0&&g2==0&&g3==0&&g4==1) {
                 shanghaizhi=2;
                 cp="zhadan";
                 guanjianzhi=chupai.get(0);
             }
             else if (g1>=5&&g2==0&&g3==0&&g4==0) {
                 if(list.get(0)>=3&&(list.get(list.size()-1)!=2)){
                 for (int e=0;e<list.size()-1;e++){
                     if(e<(chupai.size()-2))
                     {  if((chupai.get(e+1)-chupai.get(e))!=1)
                         break;
                     }
                     else if(e==(chupai.size()-2)) {
                         if ((chupai.get(chupai.size()-1)-chupai.get(chupai.size()-2))==1||(chupai.get(chupai.size()-1)-chupai.get(chupai.size()-2))==-12){
                             cp="shunzi";
                             shanghaizhi=1;
                             guanjianzhi=list.get(0);
                         }
                     }
                 }

             }
             }
             else if (g1==0&&g2>=3&&g3==0&&g4==0) {
                 if(list.get(0)>=3&&(list.get(list.size()-1)!=2)){
                 for (int e=0;e<list.size()-1;e++){
                     if(e<(chupai.size()-2))
                     {  if((chupai.get(e+1)-chupai.get(e))!=1)
                         break;
                     }
                     else if(e==(chupai.size()-2)) {
                         if ((chupai.get(chupai.size()-1)-chupai.get(chupai.size()-2))==1||(chupai.get(chupai.size()-1)-chupai.get(chupai.size()-2))==-12){
                             cp="liandui";
                             shanghaizhi=1;
                             guanjianzhi=list.get(0);
                         }
                     }
                 }
                 }
             }
             else if (g1>=1&&g2==0&&g3>=1&&g4==0) {
                 int b=1,d=1;
                  if(g1==g3)
                  {  for(int a=0;a<chupai.size();a++)
                     {   if(a<chupai.size()-1)
                        {   if(Objects.equals(chupai.get(a), chupai.get(a + 1)))
                                   d+=1;
                            else
                                d=1;
                          }
                          if(d==3)
                          {    if(b==1)
                                guanjianzhi=chupai.get(a);
                             else if(b>1)
                              {
                                  if(chupai.get(a)==2)
                                      b-=1;
                               }
                              if((a+4)<(chupai.size())) {
                                  if ((chupai.get(a + 4) - chupai.get(a)) == 1 || (chupai.get(a + 4) - chupai.get(a)) == -12)
                                      b += 1;
                              }
                          }

                       }
                       if(b==g3)
                       {
                           shanghaizhi=1;
                           cp="xiaofeiji";
                       }

                  }

             }
             else if (g1==0&&g2>=1&&g3>=1&&g4==0) {
                 int b=1,d=1;
                 if(g2==g3)
                 {  for(int a=0;a<chupai.size();a++)
                 {   if(a<chupai.size()-1)
                 {   if(Objects.equals(chupai.get(a), chupai.get(a + 1)))
                     d+=1;
                     else
                         d=1;
                 }
                     if(d==3)
                     {    if(b==1)
                          guanjianzhi=chupai.get(a);
                        else if(b>1)
                         {
                             if(chupai.get(a)==2)
                                 b-=1;
                         }
                          if(((a+4)<(chupai.size()))) {
                              if ((chupai.get(a + 4) - chupai.get(a)) == 1 || (chupai.get(a + 4) - chupai.get(a)) == -12)
                                  b += 1;
                          }

                     }

                 }
                     if(b==g3)
                     {
                         shanghaizhi=1;
                         cp="dafeiji";
                     }

                 }
             }
             else if (g1>=1&&g2==0&&g3==0&&g4>=1) {
                 int b=1,d=1;
                 if(g1==g4)
                 {  for(int a=0;a<chupai.size();a++)
                 {   if(a<chupai.size()-1)
                 {   if(Objects.equals(chupai.get(a), chupai.get(a + 1)))
                     d+=1;
                     else
                         d=1;
                 }
                     if(d==4)
                     {   if(b==1)
                         guanjianzhi=chupai.get(a);
                        else if(b>1)
                         {
                             if(chupai.get(a)==2)
                                 b-=1;
                         }
                        if(((a+5)<(chupai.size()))) {
                            if ((chupai.get(a + 5) - chupai.get(a)) == 1 || (chupai.get(a + 5) - chupai.get(a)) == -12)
                                b += 1;
                        }

                     }

                 }
                     if(b==g4)
                     {
                         shanghaizhi=1;
                         cp="4dai2";
                     }

                 }
             }
             else if (g1==0&&g2>=1&&g3==0&&g4>=1) {
                 int b=1,d=1;
                 if(g2==g4)
                 {  for(int a=0;a<chupai.size();a++)
                 {    if(a<chupai.size()-1)
                   {   if(Objects.equals(chupai.get(a), chupai.get(a + 1)))
                         d+=1;
                        else
                         d=1;
                   }
                     if(d==4)
                     {   if(b==1)
                         guanjianzhi=chupai.get(a);
                        else if(b>1)
                         {
                             if(chupai.get(a)==2)
                                 b-=1;
                         }
                         if(((a+5)<(chupai.size()))) {
                             if ((chupai.get(a + 5) - chupai.get(a)) == 1 || (chupai.get(a + 5) - chupai.get(a)) == -12)
                                 b += 1;
                         }


                     }

                 }
                     if(b==g4)
                     {
                         shanghaizhi=1;
                         cp="4dai2dui";
                     }

                 }
             }


                wanfaa wanfaa=wanfadao.cha(fangjianid);
               if(shanghaizhi>wanfaa.getShanghaizhi())
                         z=1;
               else if((shanghaizhi==wanfaa.getShanghaizhi())&&shanghaizhi!=0)
               {
                   List<Integer> dipai=new ArrayList<>();
                    String[] dpps=wanfaa.getChupai().split(",");
                    for(String dpp:dpps)
                        dipai.add(Integer.parseInt(dpp));

                   if(chupai.size()==dipai.size()){
                        if(Objects.equals(cp, wanfaa.getChupaileixing()))
                          {    if(guanjianzhi>=3&&wanfaa.getGuanjianzhi()>=3)
                                    {
                                        if(guanjianzhi>wanfaa.getGuanjianzhi())
                                                  z=1;
                                        }
                                else if(guanjianzhi<3&&wanfaa.getGuanjianzhi()<3) {
                                     if(guanjianzhi>wanfaa.getGuanjianzhi())
                                         z=1;
                                                   }
                                    else if(guanjianzhi<3&&wanfaa.getGuanjianzhi()>=3)
                                         z=1;
                                      else if(guanjianzhi>=3&&wanfaa.getGuanjianzhi()<3)
                                         z=0;


                           }

                    }
               }


               if(z==1)
               {
                  wanfadao.updatedipai(fangjianid,guanjianzhi,shanghaizhi,cp);
                  if(shanghaizhi==2)
                      wanfadao.updatejiesuandou(fangjianid,wanfadao.cha(fangjianid).getJiesuandou()*2);
                      else if(shanghaizhi==3)
                      wanfadao.updatejiesuandou(fangjianid,wanfadao.cha(fangjianid).getJiesuandou()*3);
               }

        return z;
    }

    @Override
    public List<liaotianneirong> chaliaotianneirong(int fangjianid) {
               return wanfadao.chaliaotianneirong(fangjianid);

    }
    @Override
    public void jiaoliu(int fangjianid,String zhang,String neirong){
        wanfadao.jiaoliu(fangjianid,zhang,neirong);
    }


}
